﻿using Entitas;
/// <summary>
///  reads movement input keys and plays running animation for player.
/// </summary>
class RunAnimationSystem : IExecuteSystem
{
    PlayerContext playerContext;
    public RunAnimationSystem(Contexts contexts)
    {
        playerContext = contexts.player;
    }
    public void Execute()
    {
        if (playerContext.count == 1)
        {
            var player = playerContext.GetEntities().SingleEntity<PlayerEntity>();

            //only if the player is NOT attacking can they move
            if (player.isAttacking == false&&player.hasAnimator)
            {
                //all keys pressed at same time
                if (ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.DownKeyHeld && ServiceManager.InputService.LeftKeyHeld
                  && ServiceManager.InputService.RightKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Down_IDLE");
                }
                //right

                else if (ServiceManager.InputService.RightKeyHeld && (!ServiceManager.InputService.UpKeyHeld && !ServiceManager.InputService.DownKeyHeld
                    && !ServiceManager.InputService.LeftKeyHeld))
                {
                    player.animator.Value.PlayAnimation("Run_Right");
                }
                //right-(Up-Down)
                else if (ServiceManager.InputService.RightKeyHeld && ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.DownKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Right");
                }


                //left
                else if (ServiceManager.InputService.LeftKeyHeld && (!ServiceManager.InputService.UpKeyHeld && !ServiceManager.InputService.DownKeyHeld
                    && !ServiceManager.InputService.RightKeyHeld))
                {
                    player.animator.Value.PlayAnimation("Run_Left");
                }
                //left-(up-down)
                else if (ServiceManager.InputService.LeftKeyHeld && ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.DownKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Left");
                }
                // up
                else if (ServiceManager.InputService.UpKeyHeld && (!ServiceManager.InputService.DownKeyHeld && !ServiceManager.InputService.LeftKeyHeld
                    && !ServiceManager.InputService.RightKeyHeld))
                {
                    player.animator.Value.PlayAnimation("Run_Up");
                }
                //down
                else if (ServiceManager.InputService.DownKeyHeld && (!ServiceManager.InputService.UpKeyHeld && !ServiceManager.InputService.LeftKeyHeld
                    && !ServiceManager.InputService.RightKeyHeld))
                {
                    player.animator.Value.PlayAnimation("Run_Down");
                }
                //up-right
                else if (ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.RightKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Up");
                }
                //up-left
                else if (ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.LeftKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Up");
                }

                //down-left
                else if (ServiceManager.InputService.DownKeyHeld && ServiceManager.InputService.LeftKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Down");
                }

                //down-right
                else if (ServiceManager.InputService.DownKeyHeld && ServiceManager.InputService.RightKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Down");
                }
                else if (ServiceManager.InputService.LeftKeyHeld && ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.DownKeyHeld)
                {
                    player.animator.Value.PlayAnimation("Run_Left");
                }

                //LEFT-RIGHT CASE
                else if (ServiceManager.InputService.LeftKeyHeld && ServiceManager.InputService.RightKeyHeld)
                {
                    var currentdirection = player.direction.value;
                    if (currentdirection == Direction.Left)
                        player.animator.Value.PlayAnimation("Run_Left_IDLE");
                    else
                    {
                        player.animator.Value.PlayAnimation("Run_Right_IDLE");
                    }
                }
                //UP-DOWN CASE
                else if (ServiceManager.InputService.UpKeyHeld && ServiceManager.InputService.DownKeyHeld)
                {
                    var CurrentDirection = player.direction.value;
                    if (CurrentDirection == Direction.Up)
                        player.animator.Value.PlayAnimation("Run_Up_IDLE");
                    else
                    {
                        player.animator.Value.PlayAnimation("Run_Down_IDLE");
                    }
                }

                ///keys are released 

                
                if (ServiceManager.InputService.UPkeyReleased)
                {
                    player.animator.Value.PlayAnimation("Run_Up_IDLE");
                }

                if (ServiceManager.InputService.DownkeyReleased)
                {
                    player.animator.Value.PlayAnimation("Run_Down_IDLE");
                }
                if (ServiceManager.InputService.LeftkeyReleased)
                {
                    player.animator.Value.PlayAnimation("Run_Left_IDLE");
                }
                if (ServiceManager.InputService.RightkeyReleased)
                {
                    player.animator.Value.PlayAnimation("Run_Right_IDLE");
                }
            }
        }
    }
}


